package com.yhp.servlet;

import com.yhp.bean.Student;
import com.yhp.service.StudentService;
import com.yhp.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/getList")
public class GetListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数，页码
        String index = req.getParameter("pageIndex");
        //index为空则默认为第一页
        int pageIndex = index==null?1:Integer.parseInt(index);
        //获取学生姓名、学号、性别
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //转化为数字
        int sexNum = (sex == null || sex.length() == 0?-1:Integer.parseInt(sex));

        //得到每一页学生的集合
        StudentService studentService = new StudentServiceImpl();
        List<Student> stuList = studentService.getStu(pageIndex, 5,stuname,stuno,sexNum);
        //得到总共有多少学生
        int total = studentService.total(stuname,stuno,sexNum);
        //共有多少页
        int pageNum = total%5>0?total/5+1:total/5;
//        for (Student student : stuList) {
//            System.out.println(student.getStuid());
//            System.out.println(student.getStuname());
//            System.out.println(student.getSex());
//        }
        req.setAttribute("stulist",stuList);
        req.setAttribute("pageIndex",pageIndex);
        req.setAttribute("pageNum",pageNum);
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sexNum);
        System.out.println("准备发送数据");
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }
}
